Jump to content

Are strings scalar or non-scalar?  

  1. 1. Are strings scalar or non-scalar?

    • Scalar
      0
    • Non-Scalar
      0


Recommended Posts

Posted (edited)

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None. He describes non-scalar objects as objects that have internal structure such as strings. This makes sense to me since a string is, in a sense, an array of characters. I was digging a little deeper and stumbled across an article that defines a scalar object as "an object that can have a single value" and includes both strings and complex numbers. This also makes sense to me if you follow this definition, so which is it?

 

https://jpt-pynotes.readthedocs.io/en/latest/scalar-types.html

https://www.amazon.com/Introduction-Computation-Programming-Using-Python/dp/0262529629

Edited by mjbarr
Posted (edited)
45 minutes ago, mjbarr said:

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None.

This is different than the normal definition of a scalar in science.  A Vector has magnitude and direction, a Scalar has magnitude only.

Edited by Bufofrog
Posted (edited)
2 hours ago, mjbarr said:

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None. He describes non-scalar objects as objects that have internal structure such as strings. This makes sense to me since a string is, in a sense, an array of characters. I was digging a little deeper and stumbled across an article that defines a scalar object as "an object that can have a single value" and includes both strings and complex numbers. This also makes sense to me if you follow this definition, so which is it?

 

https://jpt-pynotes.readthedocs.io/en/latest/scalar-types.html

https://www.amazon.com/Introduction-Computation-Programming-Using-Python/dp/0262529629

 

2 hours ago, Bufofrog said:

This is different than the normal definition of a scalar in science.  A Vector has magnitude and direction, a Scalar has magnitude only.

 

Computing science has its own alternative dfinitions for many things including vector.

I hadn't heard the one about a scalar but the indivisibility part makes sense since it precludes complex numbers, quaternions etc, which can be dissiciated into real and imaginary parts.

However my only Discrete Maths text to define scalar say that it is simply a real number (which of course has a special meaning in computing) .

Concrete or Discrete Mathematics is the correct topic home for this issue.

 

@bufofrog

On the matter of scalar - vector -tensor classification a tensor can be said to have a magnitude and two (or more) directions. But a tensor is also a vector as defined in a Linear Analysis vector space.

Edited by studiot
Posted
2 hours ago, mjbarr said:

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None. He describes non-scalar objects as objects that have internal structure such as strings. This makes sense to me since a string is, in a sense, an array of characters. I was digging a little deeper and stumbled across an article that defines a scalar object as "an object that can have a single value" and includes both strings and complex numbers. This also makes sense to me if you follow this definition, so which is it?

Th answer depends on how you define 'scalar', which in turn depends on the programming language. And, sometimes, the context. I don't think there is an official definition in Python for what constitutes a scalar.

There are some languages where the difference is explicit in the language. For example, C has scalars and a few structured types (e.g. arrays and structs) but no in-built support for strings. So it is easy to say what is a scalar and what isn't. Similarly, Perl explicitly names scalar and non-scalar variables differently ($var vs @var).

Languages like Pythin, that have strings as a fundamental type, may teat them as either scalars or non-scalars or, as int he case of Python. A string, in Python, is basically just a List of chars, so in that sense is a non-scalar type. But strings can be manipulated as single, atomic objects, so in that sense they are scalars.

So you need to add two more options to your list: Neither and Both.

Posted

Interesting question, I had to think for a while before answering.
@Strange and @studiot already covered two important aspects, I'll try to add a few comments from my background in the area. 
In my more formal studies of computer science the definitions used by scientists were more formal and close related to Discrete Mathematics. A scalar was typically discussed an array of length one, or a 1x1 matrix. But when moving over to for instance software engineering the terms and definitions was/is affected by the sometimes less formal approach in programming environments or languages under consideration. Sometimes a "String" would mean an immutable sequence of a characters, referenced by some pointer. The fact that the pointer referenced one single value made it a scalar. This was also influenced by the fact that the String itself was immutable. (Java* and Python are two languages I think of). 
In other environments the term "String" was an array of length n (or a 1 X n matrix) containing characters. In that case a string of characters was not matching the definitions of a scalar. 

So I agree, depending on context I could select any of Yes, No, Both, Neither. But In python and related languages and situations, I think I would vote for String=Scalar if I had to choose one without knowing more about the context. That choice would be based more on experience and work rather than formal definitions from computer science / Discrete Mathematics.

 

*)https://en.wikipedia.org/wiki/Immutable_object#Java

 

Posted
On 12/30/2019 at 5:55 PM, Ghideon said:

This was also influenced by the fact that the String itself was immutable. (Java* and Python are two languages I think of). 

.NET Framework String class implementation:

https://docs.microsoft.com/en-us/dotnet/api/system.string.intern?view=netframework-4.8

"The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system."

 

Posted
On 1/2/2020 at 3:07 PM, Sensei said:

.NET Framework String class implementation:

Thanks! I did not know that. I’ve not done much in .NET and haven’t looked at the internals. I did some quick reading to refresh memory and I'll try a slightly more generalized comment:

-When characters strings in a programming language are immutable, typically internally implemented using the flyweight* pattern, then string is a scalar type in that programming language.

Examples: Strings are scalars in .NET, Python and Java. 
Example: A string is not a scalar in C. A string is an array of characters terminated by '\0' (null character)

Note: There might be exceptions to my statement and there might be other designs than flyweight that would classify strings as scalars.

 

*) References:

Flyweight is one of the design patterns documented in the Gang Of Four book: https://archive.org/details/designpatternsel00gamm/page/195

Early example from Stanford, flyweight objects in user interfaces: https://www.researchgate.net/profile/Mark_Linton2/publication/220877079_Glyphs_flyweight_objects_for_user_interfaces/links/58adbb6345851503be91e1dc/Glyphs-flyweight-objects-for-user-interfaces.pdf

 

  • 2 weeks later...
Posted

Generally, I'd agree that a string would not be classified as scalar, but consider the following.

First, ordinarily a string is manipulated via an address in memory.  The address itself is scalar, and arithmetic can be performed.  For instance, incrementing the address essentially removes characters from the beginning of the string, the number of which is equal to the increment.

Second, you could consider the string as a list of digits, base 256 for ASCII, base 65536 for Unicode.  Then, each possible string would map to a single integer.

 

Posted (edited)
47 minutes ago, Huckleberry of Yore said:

Generally, I'd agree that a string would not be classified as scalar, but consider the following.

First, ordinarily a string is manipulated via an address in memory.  The address itself is scalar, and arithmetic can be performed.  For instance, incrementing the address essentially removes characters from the beginning of the string, the number of which is equal to the increment.

Second, you could consider the string as a list of digits, base 256 for ASCII, base 65536 for Unicode.  Then, each possible string would map to a single integer.

You are talking about the most primitive implementation from C. Array of characters. In C++ and other OOP string is blackbox object which you can read and modify only by calling object method.

Edited by Sensei
Posted (edited)

There are countless implementations in C++ but ultimately the string is stored in memory and referenced by its pointer.  If a particular C++ implementation doesn't allow access directly to the memory, it does the manipulations internally using that address or allocates a new one if necessary.  Furthermore, in C++, give me a class that hides the implementation of the string and I can break it with casting and so forth.

Regardless of language, a string is a sequence of characters, so I was suggesting that if each character is treated as a digit with radix chosen according to the encoding, each string can map to an integer.  Not sure how useful that is, but it could be viewed as a scalar.

21534559H = "YES!"

Edited by Huckleberry of Yore
Punctuation.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.